//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientSetupActivate.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_action_title","_activated_msg","_deactivated_msg","_shared_variable","_initial_status","_activated_title","_deactivated_title","_activated_code","_deactivated_code"
		,"_action_id","_arguments","_event","_condition","_action_display_timer"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);

if (!isnull _object) then {

	_action_id = [_action] call FFUNC(actionGetID);
	TRACE_1("Action",_action);
	_parameters = [_action] call FFUNC(actionGetParams);

	_activated_msg = [_parameters, "ACTIVATION_MSG", [],THIS_FILE] call FFUNC(hashGet);
	_deactivated_msg = [_parameters, "DEACTIVATION_MSG", [],THIS_FILE] call FFUNC(hashGet);
	_shared_variable = [_parameters, "SHARED_VAR", "",THIS_FILE] call FFUNC(hashGet);
	_initial_status = [_parameters, "SHARED_VAR_VAL", false,THIS_FILE] call FFUNC(hashGet);
	_activated_title = [_parameters, "ACTIVATED_TITLE", "STR_TMF_ACTIVATE_DEFACTIONON",THIS_FILE] call FFUNC(hashGet);
	_deactivated_title = [_parameters, "DEACTIVATED_TITLE", "STR_TMF_ACTIVATE_DEFACTIONOFF",THIS_FILE] call FFUNC(hashGet);
	_activated_code = [_parameters, "ACTIVATED_CODE", "",THIS_FILE] call FFUNC(hashGet);
	_deactivated_code = [_parameters, "DEACTIVATED_CODE", "",THIS_FILE] call FFUNC(hashGet);
	_shared_variable = _shared_variable call FFUNC(getStringVar);

	_object allowDamage false;

	_initial_status = [_action,_initial_status] call FFUNC(actionGetEvent);
	_action_title = _activated_title;
	If (!_initial_status) then { _action_title = _deactivated_title; };
	[_object,_action,"ACTIVATE_STATUS",_initial_status] call FFUNC(actionSetLocal);
	_act_id = [_object,_action,"ACTIVATE_ACTION",-1] call FFUNC(actionGetLocal);
	if (_act_id >= 0) then {
		TRACE_1("Remove Action",_act_id);
		_object removeAction _act_id;
	};
	if (_action_title != "") then {
		_action_title = (_action_title) call FFUNC(translate);
		_arguments = [];
		_arguments set [TMF_EXECUTE_ACTION_PARAM_ACTID, _action_id];
		_object_var = format["mission_object_activation_status_%1",_action_id];
		OSETVAR(_object,_object_var,_initial_status);
		_action_display_timer = QGVAR(action_display_timer);
		_object setVariable [_action_display_timer,0];
		_condition = format ["(alive _target) && {vehicle player == player} && {((_target getVariable '%1'))} && {(_target getVariable '%2' < time)}",_object_var,_action_display_timer];
		if (!_initial_status) then {
			_condition = format ["(alive _target) && {vehicle player == player} && {(!(_target getVariable '%1'))} && {(_target getVariable '%2' < time)}",_object_var,_action_display_timer];
		};
		_act_id = _object addAction [_action_title, QPATH(addAction.sqf),_arguments,10,True,True,"",_condition];
		// We set the variable as local because it is dependent on the computer running it.
		[_object,_action,"ACTIVATE_ACTION",_act_id] call FFUNC(actionSetLocal);
		TRACE_1("Added Action",ARR_4(_act_id,_action_title,typeof _object,_condition));
	};
};

TRACE("End");
TMFPROFILERSTOP;